
#ifndef __APR_READ_WRITE_MUTEX_H
#define __APR_READ_WRITE_MUTEX_H

#include "../iface/APRInterface.h"
#include <apr_thread_rwlock.h>

class APRRWMutex
{
   private:
      bool init(apr_pool_t *mp)
      {
         apr_status_t status;
         status = apr_thread_rwlock_create(&aprt_rwmutex, mp);
         CheckAPRError(status);
         return true;
      }
      bool destroy()
      {
         apr_status_t status;
         status = apr_thread_rwlock_destroy(aprt_rwmutex);
         CheckAPRError(status);
         return true;
      }
   protected:
      apr_thread_rwlock_t *aprt_rwmutex;
   public:
      APRRWMutex(apr_pool_t *mp) { init(mp); }
      virtual ~APRRWMutex() { destroy(); }
      bool lockRead()
      {
         apr_status_t status;
         status = apr_thread_rwlock_rdlock(aprt_rwmutex);
         //CheckAPRError(status);
         return true;
      }
      bool unlockRead() { unlock(); return true; }
      bool lockWrite()
      {
         apr_status_t status;
         status = apr_thread_rwlock_wrlock(aprt_rwmutex);
         CheckAPRError(status);
         return true;
      }
      bool unlockWrite() { unlock(); return true; }
      bool unlock()
      {
         apr_status_t status;
         status = apr_thread_rwlock_unlock(aprt_rwmutex);
         //CheckAPRError(status);
         return true;
      }
};

#endif
